package com.bizzard.system.controller;

import com.bizzard.basic.util.AjaxResult;
import com.bizzard.basic.util.PageList;
import com.bizzard.system.domain.Systemdictionarydetail;
import com.bizzard.system.domain.Systemdictionarytype;
import com.bizzard.system.query.SystemdictionarytypeQuery;
import com.bizzard.system.service.ISystemdictionarydetailService;
import com.bizzard.system.service.ISystemdictionarytypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 接口类
 */

@RestController
@RequestMapping("/systemdictionarytype")
@Api(value = "字典接口",description = "字典接口")
public class SystemdictionarytypeController {
    @Autowired
    private ISystemdictionarytypeService iSystemdictionarytypeService;

    @Autowired
    private ISystemdictionarydetailService systemdictionarydetailService;


    /**
     *  查询单个对象接口
     * @param id
     * @return
     */
    @ApiOperation(value = "查询单个对象")
    @GetMapping("/{id}")
    public Systemdictionarytype findOne (@PathVariable("id")Long id){
        return  iSystemdictionarytypeService.findById(id);

    }

    /**
     *  查询所有对象接口
     * @return
     */
    @ApiOperation(value = "查询所有对象")
    @GetMapping
    public List<Systemdictionarytype> findAll (){
        return  iSystemdictionarytypeService.findAll();

    }

    /**
     *  分页查询接口
     * @param systemdictionarytypeQuery
     * @return
     */
    @ApiOperation(value = "分页查询")
    @PostMapping
    public PageList<Systemdictionarytype> queryPage (@RequestBody SystemdictionarytypeQuery systemdictionarytypeQuery){
        return  iSystemdictionarytypeService.queryPage(systemdictionarytypeQuery);

    }

    /**
     *  删除操作接口
     * @param id
     * @return
     */
    @ApiOperation(value = "删除")
    @DeleteMapping("/{id}")
    public AjaxResult delete (@PathVariable("id") Long id){
        try {
            List<Systemdictionarydetail> list = systemdictionarydetailService.findAll();
            for(Systemdictionarydetail sy: list) {
                if (sy.getTypes_id() == id) {
                    Long id1 = sy.getId();
                    systemdictionarydetailService.del(id1);
                }
            }
            iSystemdictionarytypeService.del(id);
            return  new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }

    }




    /**
     *  添加和修改 接口
     */
    @PutMapping
    @ApiOperation(value = "添加或修改")
    public AjaxResult addOrUpdate(@RequestBody Systemdictionarytype systemdictionarytype){
        try {
            if(systemdictionarytype.getId() == null){//添加
                iSystemdictionarytypeService.add(systemdictionarytype);
            }else{
                iSystemdictionarytypeService.update(systemdictionarytype);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    @ApiOperation(value = "批量删除接口")
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
        try {
            iSystemdictionarytypeService.patchDelete(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"批量删除失败");
        }
    }


}
